package cn.gdcp.democ;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class MyAdapter extends BaseAdapter  {
    private ArrayList<Score> scoreArrayList=new ArrayList<>();
    private Context context;
    private DelScoreListen delScoreListen;

    public MyAdapter(ArrayList<Score> scoreArrayList, Context context,DelScoreListen delScoreListen) {
        this.scoreArrayList = scoreArrayList;
        this.context = context;
        this.delScoreListen=delScoreListen;
    }

    @Override
    public int getCount() {
        return scoreArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return scoreArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

         if (view==null){
                        view= LayoutInflater.from(context).inflate(R.layout.score_list,null,false);
                        ScoreHolder scoreHolder=new ScoreHolder();
                        scoreHolder.name=view.findViewById(R.id.name);
                        scoreHolder.score_android1=view.findViewById(R.id.score_android1);
                        scoreHolder.score_android2=view.findViewById(R.id.score_android2);
                        scoreHolder.score_english1=view.findViewById(R.id.score_english1);
                        scoreHolder.score_english2=view.findViewById(R.id.score_english2);
                        scoreHolder.del_score=view.findViewById(R.id.del_score);
                        view.setTag(scoreHolder);
         }
                  ScoreHolder scoreHolder= (ScoreHolder) view.getTag();
                  final Score score=scoreArrayList.get(i);
                        scoreHolder.name.setText(score.getName());
                        scoreHolder.score_android2.setText(String.valueOf(score.getAndroid()));
                        scoreHolder.score_english2.setText(String.valueOf(score.getEnglish()));
                        scoreHolder.del_score.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                 delScoreListen.del_Score(score);
                            }
                        });

        return view;
    }
}
